/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.far.aus.adm;

import com.far.aus.adm.clas.FormaPago;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Jorge
 */
public class FrmFormaPago extends HttpServlet {

    private String _ip = null;
    private int _puerto = 1433;
    private String _db = null;
    private String _usuario = null;
    private String _clave = null;
    
    public void init(ServletConfig config) throws ServletException
    {
        this._ip = config.getServletContext().getInitParameter("_IP");
        this._puerto = Integer.parseInt(config.getServletContext().getInitParameter("_PUERTO"));
        this._db = config.getServletContext().getInitParameter("_DB");
        this._usuario = config.getServletContext().getInitParameter("_USUARIO");
        this._clave = config.getServletContext().getInitParameter("_CLAVE");
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "Mon, 01 Jan 2001 00:00:01 GMT");
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Cache-Control", "must-revalidate");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();

        String id = request.getParameter("id");
        
        FormaPago objFormaPago = new FormaPago(this._ip, this._puerto, this._db, this._usuario, this._clave);
        
        String msg = "";
        String forma_pago = "";
        int estado = 1;
        if(id.compareTo("-1")!=0){
            try{
                ResultSet rs = objFormaPago.getTipoFormaPago(id);
                if(rs.next()){
                    forma_pago = rs.getString("forma_pago")!=null ? rs.getString("forma_pago") : "";
                    estado = (rs.getString("estado")!=null) ? rs.getInt("estado") : 1;
                    rs.close();
                }
            }catch(Exception e){
                msg = e.getMessage();
            }
        }
        
        try {
            out.print("obj»d_3^foc»forma_pago^msg»"+msg+"^frm»");
            out.print("<form action=\"FrmFormaPagoGuardar\" onsubmit=\"return aus_tipo_forma_pagos_guardar(this)\" autocomplete=\"off\">");
            out.print("<input type=\"hidden\" id=\"id\" name=\"id\" value=\""+id+"\" />");
            out.print("<h3>TIPO DE FORMA DE PAGO</h3>");
            out.print("<table>");
            out.print("<tr>");
            out.print("<td>Nombre de forma de pago: <span class=\"marca\">*</span></td>");
            out.print("<td><input type=\"text\" id=\"forma_pago\" name=\"forma_pago\" size=\"40\" maxlength=\"160\" value=\""+forma_pago+"\" /></td>");
            out.print("</tr>");
            out.print("<tr>");
            out.print("<td>Estado: <span class=\"marca\">*</span> </td>");
            out.print("<td><label><input type='radio' id='e0' name='estado' value='1' "+((estado==1) ? "checked" : "")+" /> Activo</label> &nbsp;&nbsp;&nbsp; " +
                    "<label><input type='radio' id='e1' name='estado' value='0' "+((estado==0) ? "checked" : "")+" /> Inactivo</label></td>");
            out.print("</tr>");
            out.print("<tr>");
            out.print("<td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"Guardar\" /></td>");
            out.print("</tr>");
            out.print("</table>");
            out.print("</form>");
        } finally {
            objFormaPago.cerrar();
            out.close();
        }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
